package com.news.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.news.bean.Add_admin;
import com.news.dao.Add_adminDao;
import com.news.util.DButil;
import com.sun.jndi.cosnaming.CNNameParser;

public class Add_adminDaoImpl implements Add_adminDao{
	private final String LOGIN = "select count(*) from add_admin where account_no=?and password=?";
        private final String TOTAL = "select count(*) from add_admin";
	private final String SHOWUSER = "select * from add_admin";
	private final String ADDUSER = "insert into add_admin(account_no,password) values(?,?)";
	private final String DELUSER = "delete from add_admin where id=?";
	private final String EDITUSER = "update add_admin set account_no=?,password=? where id=?";
	//登录实现
	public boolean login(String account_no,String password) {
		int ret = 0;
		Connection conn = DButil.getConnection();
		try {
			PreparedStatement pstmt = conn.prepareStatement(LOGIN);
			pstmt.setString(1, account_no);
			pstmt.setString(2, password);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				ret = rs.getInt(1);
                                System.out.println("retttt"+ret);
			}
                        if(ret==0){
                            
                         return false;
                        }else{
                          return true;
                        }
                       // System.out.println("return true");
                      //  return true;

		} catch (SQLException e) {
			e.printStackTrace();
                        System.out.println("return false");
                        return false;
		}
		
	}

        public int totaluser(){
            	int ret = 0;
		PreparedStatement pstmt = DButil.getPreparedStatement(TOTAL);
		try {
		
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
        }
	//显示列表
	public List<Add_admin> showuser(Map<?, ?> map) {
		int i = 0,ret = 0;
		List<Add_admin> list = new ArrayList<Add_admin>();
		try {
		
			PreparedStatement pstmt = DButil.getPreparedStatement(SHOWUSER);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				Add_admin ad = new Add_admin();
				ad.setId(rs.getInt("id"));
				ad.setAccount_no(rs.getString("account_no"));
				ad.setPassword(rs.getString("password"));
				list.add(ad);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DButil.close();
		}
		return list;
	}
	//添加管理员
	public int adduser(Add_admin ad) {
		int i = 1, ret = 0;
		//Add_admin ad = new Add_admin();
		Connection conn = DButil.getConnection();
		try {
			PreparedStatement pstmt = conn.prepareStatement(ADDUSER);
			//pstmt.setInt(i++, ad.getId());
			pstmt.setString(i++, ad.getAccount_no());
			pstmt.setString(i++, ad.getPassword());
			ret = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DButil.close();
		}
		
		return ret;
	}
	//删除管理员
	public int deluser(Integer id) {
		int i = 0,ret = 0;
		Connection conn = DButil.getConnection();
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			PreparedStatement pstmt = conn.prepareStatement(DELUSER);
			pstmt.setInt(1, id);//按id删除
			ret = pstmt.executeUpdate();
			
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
	}
	//编辑管理员
	public int edituser(Add_admin ad) {

		int i = 1,ret = 0;
		try {
			PreparedStatement pstmt = DButil.getPreparedStatement(EDITUSER);
			pstmt.setString(i++, ad.getAccount_no());
			pstmt.setString(i++, ad.getPassword());
			pstmt.setInt(i++, ad.getId());
			ret = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DButil.close();
		}
		return ret;
	}
	


}
